package com.iyoudoock.citymine.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.ArrayList;

/**
 * Created by Kvin on 2017/5/23.
 */
public class SystemUtils {

    /**
     * @param context
     * @return 返回本机的MAC地址ַ作为唯一标识
     */
    public static String getMac(Context context) {
        WifiManager wifi = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        String mac = wifi.getConnectionInfo().getMacAddress();
        return mac.replaceAll(":", "");
    }

    /**
     * get version name
     */
    public static String getVersionName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            StringBuilder sb = new StringBuilder();
            sb.append("V.").append(packInfo.versionName);
            return sb.toString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * check whether sim is ready
     *
     * @param context
     * @return
     */
    public boolean isSimMounted(Context context) {
        TelephonyManager manager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
        return manager.getSimState() == TelephonyManager.SIM_STATE_READY;
    }

    /**
     * call
     */
    public static void call(Activity from, String phoneNumber) {
        try {
            Intent mIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
            if (ActivityCompat.checkSelfPermission(from, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            from.startActivity(mIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String[] convertToArray(String src, String symbol) {
        if (TextUtils.isEmpty(src)) return null;
        return src.split(symbol);
    }

    public static String[] convertToArray(String src) {
        return convertToArray(src, "-");
    }

}
